/*************************************************************************
 * CS516 Software Engineering
 * Group Project: Android Finance Management System
 * AddTransaction.java
 * The tab page to list transaction
 *************************************************************************/
package com.svu.se.group3;

import java.util.ArrayList;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
/**
 * (add your name below)
 * @author A2303 Lei Jiang 
 */
public class ListTransaction extends ListActivity {

	private ProgressDialog mProgressDialog = null;
    private ArrayList<Transaction> mTransactions = null;
    private TransactionAdapter mAdapter;
    private Runnable viewTransacitons;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.list_transactions);
    }
    
    @Override
    protected void onResume(){
    	super.onResume();
    	
    	mTransactions = new ArrayList<Transaction>();
        this.mAdapter = new TransactionAdapter(this, R.layout.row, mTransactions);
        setListAdapter(this.mAdapter);
       
        viewTransacitons = new Runnable(){
            @Override
            public void run() {
                // retrieve transaction data
            	getTransactions();
            }
        };
        
        Thread thread =  new Thread(viewTransacitons);
        thread.start();
        
        // Show Progress Dialog
        mProgressDialog = new ProgressDialog(ListTransaction.this);
        mProgressDialog.setMessage(ListTransaction.this.getString(R.string.tab_list_processing));
        mProgressDialog.show();
    }
	
	private Runnable returnRes = new Runnable() {
        @Override
        public void run() {
            if(mTransactions != null && mTransactions.size() > 0){
            	mAdapter.notifyDataSetChanged();
                for(int i=0;i<mTransactions.size();i++)
                	mAdapter.add(mTransactions.get(i));
            }
            mProgressDialog.dismiss();
            mAdapter.notifyDataSetChanged();
        }
	};
    
	private void getTransactions(){
        mTransactions = Transaction.getbyDateCategory(null, null);
        runOnUiThread(returnRes);
      }
	
	private class TransactionAdapter extends ArrayAdapter<Transaction> {
		private ArrayList<Transaction> items;

		public TransactionAdapter(Context context, int textViewResourceId, ArrayList<Transaction> objects) {
			super(context, textViewResourceId, objects);
			// TODO Auto-generated constructor stub
			this.items = objects;
		}
		
		@Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.row, null);
                }
                Transaction o = items.get(position);
                if (o != null) {
                    // Fill transaction information
                	TextView mRowName = (TextView) v.findViewById(R.id.row_name);
                	TextView mRowCategory = (TextView) v.findViewById(R.id.row_category);
                	TextView mRowTotal = (TextView) v.findViewById(R.id.row_total);
                	TextView mRowDate = (TextView) v.findViewById(R.id.row_date);
                	TextView mRowId = (TextView) v.findViewById(R.id.row_id);
                    if (mRowName != null){
                    	mRowName.setText(o.getmProductName());
                    }
                    if (mRowCategory != null){
                    	mRowCategory.setText(o.getmCategory());
                    }
                    if (mRowTotal != null){
                    	mRowTotal.setText(o.getTotal());
                    }
                    if (mRowDate != null){
                    	mRowDate.setText(o.getmPurchaseDate());
                    }
                    if (mRowId != null){
                    	mRowId.setText(Integer.toString(o.getId()));
                    }
                }
                return v;
        }
	}

	

}

